//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
Page({
  data: {
    list: [],
    date: ''
  },
  bindTest: function () {
    util.setNewCalendar()
    // app.globalData.calendarList.push({
    //   date: '',
    //   timeList: []
    // })
    console.log(app.globalData.calendarList)
  },
  getNextTime: function (time, hour, minute) {

  },
  addTimeList: function () {
    var that = this
    var list = that.data.list
    list.push(util.setNewTimeList(list))
    // list = util.changeList(list)
    that.setData({
      list: list
    })
    // util.saveInfoToApp(list[list.length - 2].schedule)
    // console.log(list)
  },
  // 删除一条日程
  deleteTimeList: function (e) {
    var that = this
    var list = that.data.list
    list.push(util.setNewTimeList(list))
    // list = util.changeList(list)
    that.setData({
      list: list
    })
    // util.saveInfoToApp(list[list.length - 2].schedule)
    // console.log(list)
  },
  // 开始时间修改
  startTimeChange: function (e) {
    var item = this.getItemByIndex(e)
    item.startTime = e.detail.value
    this.updateListByItemAndE(e, item)
    // var index = e.currentTarget.dataset.index
    var list = this.data.list
    // if (index != 0) {
    //   var last = e
    //   last.currentTarget.dataset.index = e.currentTarget.dataset.index - 1
    //   this.endTimeChange(last)
    // }
    this.setData({
      list: list
    })
  },
  // 结束时间修改
  endTimeChange: function (e) {
    var item = this.getItemByIndex(e)
    item.endTime = e.detail.value
    this.updateListByItemAndE(e, item)
    // 如果是最后一项，则增加一行
    if (item.endTime != '23:59') {
      var index = e.currentTarget.dataset.index
      var list = this.data.list
      if (list.length == index + 1) {
        this.addTimeList()
      } else {
        var next = e
        next.currentTarget.dataset.index = e.currentTarget.dataset.index + 1
        this.startTimeChange(next)
      }
    }
    this.setData({
      list: list
    })
    // if (e.currentTarget.dataset.index - 1 == this.data.list.length) {
    // this.addTimeList()
    // }
  },
  // 输入做的事情
  scheduleInput: function (e) {
    var item = this.getItemByIndex(e)
    item.schedule = e.detail.value
    this.updateListByItemAndE(e, item)
    // util.saveInfoToApp(e.detail.value)
  },
  // 修改数组项
  updateListByItemAndE: function (e, item) {
    var key = 'this.data.list[' + e.currentTarget.dataset.index + ']'
    item.duration = util.getDValueOfTime(item)
    this.setData({
      [key]: item
    })
  },
  // 根据传入的index定位数组项和key
  getItemByIndex: function (e) {
    return this.data.list[e.currentTarget.dataset.index]
  },
  saveInfo: function () {
    console.log(this.data.list)
    var list = this.valueFormat(this.data.list)
    util.saveInfo(this.data.date, list)
    this.setData({
      list: list
    })
  },
  // 删除数组中不合适的条目
  valueFormat: function (list) {
    list.forEach(function (item, index) {
      if (item.schedule == '' || item.schedule == null) {
        list.splice(index, 1)
      }
    })
    return list
  },
  dateChange: function(e) {
    var date = e.detail.value
    this.setData({
      date: date
    })
    this.getListByDate(date)
  },
  // 重置信息，清空缓存
  resetList: function () {
    util.saveInfo(this.data.date, [])
    this.getListByDate(this.data.date)
  },
  getListByDate: function(date) {
    var list = util.getInfo(date)
    console.log('getlist', list)
    if (list != null) {
      this.setData({
        list: list
      })
    }
  },
  onLoad: function () {
    var date = util.getDate(new Date)
    this.setData({
      date: date
    })
    this.getListByDate(date)
    console.log('list:',this.data.list)
  },
  onShow: function () {

  }
})
